<?php
// +----------------------------------------------------------------------
// | Action ：门票模块
// +----------------------------------------------------------------------
// | DateTime：2015年12月8日
// +----------------------------------------------------------------------
// | Author: Danson <zoudan916@163.com>
// +----------------------------------------------------------------------

namespace Home\Controller;

class TicketsController extends WapController{

    public function action(){
        return 'Tickets/index';
    }

    /* 购票首页 */
    public function index(){
        $this->display();
    }

    /* 群主购买门票 */
    public function order(){
        $order = M('order')->where(array('openid'=>$_SESSION['wx_user']['openid'],'status'=>20))->find();
        $tickets = M('tickets')->where(array('tickets_id'=>2))->find();
        $user = M('lord_user')->where(array('openid'=>$_SESSION['wx_user']['openid']))->find();
        if(empty($user)){
            $price = $tickets['tickets_price'];
            $type = 0;
        }else{
            $type = 1;
            $price = $tickets['vip_price'];
        }
        if(isset($_GET['type'])){
            if($_GET['type'] == 0){
                $type = 0;
                $price = $tickets['tickets_price'];
            }else{
                $type = 1;
                $price = $tickets['vip_price'];
            }
        }
        //sign已填,商户无需重复填写
        Vendor('Wxpay.WxPayPubHelper.WxPayPubHelper');
        $jsApi = new \JsApi_pub();
        //=========步骤1：网页授权获取用户openid============
        $openid = $_SESSION['wx_user']['openid'];
        //=========步骤2：使用统一支付接口，获取prepay_id============
        //使用统一支付接口
        $unifiedOrder = new \UnifiedOrder_pub();
        $unifiedOrder->setParameter("openid","$openid");//商品描述
        $unifiedOrder->setParameter("body",'2015重庆社群群主大会');//商品描述
        //自定义订单号，此处仅作举例
        $timeStamp = time();
        $out_trade_no = \WxPayConf_pub::APPID."$timeStamp";
        $unifiedOrder->setParameter("out_trade_no",$out_trade_no);//商户订单号
        $unifiedOrder->setParameter("total_fee",intval($price).'00');//总金额
        $unifiedOrder->setParameter("notify_url",\WxPayConf_pub::NOTIFY_URL);//通知地址
        $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
        $prepay_id = $unifiedOrder->getPrepayId();
        $all['order_sn'] = build_order_no();
        $all['pay_sn']   = $out_trade_no;
        $all['openid']   = $_SESSION['wx_user']['openid'];
        $all['nickname']   = $_SESSION['wx_user']['nickname'];
        $all['user_name']   = $user['user_name'];
        $all['user_phone']   = $user['user_phone'];
        $all['status']   = 10;
        $all['price']    = $price;
        $all['create_time'] = time();
        M('order')->add($all);
        //=========步骤3：使用jsapi调起支付============
        $jsApi->setPrepayId($prepay_id);
        $jsApiParameters = $jsApi->getParameters();
        $this->assign("jsApiParameters",$jsApiParameters);
        $this->assign('info',$tickets);
        $this->assign('user',$user);
        $this->assign('out_trade_no',$out_trade_no);
        $this->assign('type',$type);
        $this->assign('order',$order);
        $this->display();
    }

    /* 支付成功 */
    public function apply(){
        $map['pay'] = $_POST['pay'];
        $map['openid'] = $_SESSION['wx_user']['openid'];
        $info = M('order')->where($map)->find();
        M('order')->where($map)->save($_POST);
        send_sms($info['user_phone'],'恭喜您！重庆社群群主大会通票购买成功，您的兑票验证码：'.$info['order_sn'].'，请妥善保管切勿外泄，并于26日早上8:30-9:30内于花木世界现场取票处兑票，谢谢您的参与。');
        $this->display();
    }
}